@extends('layout.master')

@section('title','修改资讯')

@section('css')
    @parent
    <link href="{{ asset('plugins/bower_components/summernote/dist/summernote.css') }}" rel="stylesheet"/>
    <!-- animation CSS -->
    <link href="{{ asset('css/animate.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/icheck/skins/all.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/bootstrap-switch/bootstrap-switch.min.css') }}" rel="stylesheet">
@endsection
@section('main')
    @if (session('message'))
        <div>
            <input type="hidden" id="hidden" name="hidden" value="hidden">
            <input type="hidden" id="message" name="message" value="{{ session('message') }}">
            <input type="hidden" id="status" name="status" value="{{ session('status') }}">
        </div>
    @endif
    <div class="white-box">
        <form class="form-horizontal form-material" method="post" id='formid' enctype="multipart/form-data">
		 <input type="hidden" id="id-modify" value="{{ $advice->id }}">
            <div class="row">
                <div class="col-md-6 col-sm-4">
                    <div class="form-group">
                        <label class="col-md-12">标题</label>
                        <div class="col-md-12">
                            <input type="text" name="title" id="title"  value="{{ $advice->title }}"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>
			</div>	
			<div class="row">	
                <div class="col-md-2 col-sm-4">
                    <label class="control-label">所属栏目</label>
                    <div class="example">
                        <select id="column_id" name="column_id">
							@foreach(DB::table('kl_column_dic')->select('id', 'column_name')->where('is_del','1')->get() as $value)
                                <option value="{{ $value->id}}" @if($value->id === $advice->column_id) selected
                                        @endif>{{ $value->column_name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="col-md-2 col-sm-4">
                    <label class="control-label">健康号</label>
                    <div class="example">
                        <select id="health_num_id" name="health_num_id">
							@foreach(DB::table('kl_healthy_num')->select('id', 'name')->get() as $value)
                                <option value="{{ $value->id . '||' . $value->name }}" @if($value->id === $advice->health_num_id) selected
                                        @endif>{{ $value->name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>


			
                <div class="col-md-2 col-sm-4">
                    <label class="control-label">是否删除</label>
                    <div class="example">
                        <select id="is_del" name="is_del">
                            @foreach(App\ZxLists::$DelDisplay as $key => $value)
                                <option value="{{ $key }}"  @if($key === $advice->is_del) selected
                                        @endif>{{ $value }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
				
                <div class="col-md-2 col-sm-4">
                    <label class="control-label">类型</label>
                    <div class="example">
                        <select id="type" name="type">
                            @foreach(App\ZxLists::$TypeDisplay as $key => $value)
                                <option value="{{ $key }}" @if($key === $advice->type) selected
                                        @endif>{{ $value }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
			</div>	
			<div class="row">
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">封面图</label>
					<a class="fcbtn btn-success btn-outline btn-1e waves-effect"
                       @if(isset($cover[0])) onclick="picDel({{ $cover[0]->id }})" @endif>删除此图</a>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="cover0" class="dropify" name="cover[0]"
                                   data-height="200px" @if(isset($cover[0])) data-default-file="{{ $cover[0]->pic_path }}" @endif />
							<input type="hidden" id="pic0" name="pics[]" @if(isset($cover[0])) value="{{ $cover[0]->pic_path }} @endif" />
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">封面图</label>
					<a class="fcbtn btn-success btn-outline btn-1e waves-effect"
                       @if(isset($cover[1])) onclick="picDel({{ $cover[1]->id }})" @endif>删除此图</a>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="cover1" class="dropify" name="cover[1]"  @if(isset($cover[1])) data-default-file="{{ $cover[1]->pic_path }}" @endif
                                   data-height="200px"/>
							<input type="hidden" id="pic1" name="pics[]" @if(isset($cover[1])) value="{{ $cover[1]->pic_path }}" @endif />
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">封面图</label>
					<a class="fcbtn btn-success btn-outline btn-1e waves-effect"
                       @if(isset($cover[2])) onclick="picDel({{ $cover[2]->id }})" @endif>删除此图</a>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="cover2" class="dropify" name="cover[2]"
							 @if(isset($cover[2]))
                                   data-default-file="{{ $cover[2]->pic_path }}"
                                   @endif
                                   data-height="200px"/>
							<input type="hidden" id="pic2" name="pics[]" @if(isset($cover[2])) value="{{ $cover[2]->pic_path }}" @endif />
                        </div>
                    </div>
                </div>
			</div>	
			<div class="row">
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">视频封面图</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="video_screenshots" class="dropify" name="video_screenshots"
                                   data-height="200px" data-default-file="{{$advice->video_screenshots}}"/>
								   <input type="hidden" value="{{$advice->video_screenshots}}" id="fengmian">
                        </div>
                    </div>
                </div>
                  <div class="col-md-4 col-sm-4">
                    @if(!empty($advice->video))<label class="control-label">如果您想更换视频，请在这里选择新的视频</label>@endif
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="video" class="dropify" name="video"
                                   data-height="200px"/>
							 <input type="hidden" value="{{$advice->video}}" id="shipin">
                        </div>
                    </div>
                </div> 
				@if(!empty($advice->video))
				<video controls style="width:23%;height:89%;margin-top: 30px;">
					<source src="{{ $advice->video }}" type="video/mp4">
					<source src="{{ $advice->video }}" type="video/ogg">
					<source src="{{ $advice->video }}" type="video/webm">
					<object data="{{ $advice->video }}">
						<embed src="{{ $advice->video }}">
					</object>
				</video>	
				@endif				
            </div>
            <div class="row">
                <div class="col-md-12 col-sm-12">
                    <textarea name="content" class="form-control" id="summernote" cols="30" rows="5">{{ $advice->content }}</textarea>
					<textarea name="contents" id="contents" style="display:none;">{{ $advice->content }}</textarea>
					
                </div>
            </div>

        
        <div class="form-group">
            <button type="button" class="fcbtn btn btn-success btn-outline btn-1e waves-effect" id="publish">修改
            </button>

            {!! csrf_field() !!}
            <a class="fcbtn btn btn-info btn-outline btn-1e waves-effect" href="/zixun/zxlist">返回
            </a>
        </div>
	</form>
    </div>

@endsection
@section('js')
    @parent
    <script>
        $(document).ready(function () {
            $('#summernote').summernote();
        });
        $('#summernote').summernote({
            height: 500,
            toolbar: [
                // [groupName, [list of button]]
                ['style', ['bold', 'italic', 'underline', 'clear']],
                ['fontsize', ['fontsize']],
                ['fontname', ['fontname']],
                ['color', ['color']],
                ['para', ['ul', 'ol', 'paragraph']],
                ['height', ['height']],
                ['insert', ['link', 'picture', 'video']],
                ['view', ['fullscreen', 'codeview', 'help']]
            ],
            lang: 'zh-CN',
            placeholder: '内容',
            dialogsFade: true,
            callbacks: {
                onImageUpload: function (files) {
                    var data = new FormData();
                    data.append("file", files[0]);
                    $.ajax({
                        data: data,
                        type: "POST",
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: "/upload", /*填写后台上传文件的路径*/
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function (url) {/*url为上传成功后返回的图片路径*/
                            var imgNode = document.createElement('img');
                            imgNode.src = url+'?x-oss-process=image/resize,w_500';
                            $('#summernote').summernote('insertNode', imgNode);
                        }
                    })
                }
            }
        });
    </script>

    <script>
        $("#publish").click(function (e) {
			var data = $("#summernote").val();
			var title = $("#title").val();
			$("#contents").html(data);
            var file0 = document.getElementById("cover0").files;
            var file1 = document.getElementById("cover1").files;
            var file2 = document.getElementById("cover2").files;
			var video_screenshots = document.getElementById("video_screenshots").files;
			var video = document.getElementById("video").files;
			var shipin = $("#shipin").val();
			var fengmian = $("#fengmian").val();
			//图片
			var falg0=1;
			var falg1=1;
			var falg2=1;
			//视频
			var falg3=1;
			var falg4=1;


			if(title==''){
                 swal("标题不能为空!");
                return false;				
			}
			if(data==''){
                 swal("内容不能为空!");
                return false;				
			}

			
			if($("#pic0").val()==''){
				if (file0.length) {
					if ((file0[0].type).indexOf("image/") === -1) {
						falg0 = 2;
						swal("请上传图片!");
						return false;
					}

					$("#pic0").val(picajax(file0[0]));
				}else{
					falg0 = 2;
				}
			}

			if($("#pic1").val()==''){
				if (file1.length) {
					if ((file1[0].type).indexOf("image/") === -1) {
						falg1 = 2;
						swal("请上传图片!");
						return false;
					}
					 $("#pic1").val(picajax(file1[0]));
				}else{
					falg1 = 2;
				}
			}
			if($("#pic2").val()==''){
				if (file2.length) {
					if ((file2[0].type).indexOf("image/") === -1) {
						falg2 = 2;
						swal("请上传图片!");
						return false;
					}
					$("#pic2").val(picajax(file2[0]));
				}else{
					falg2 = 2;
				}
			}
			if(fengmian.length===0){
				if (video_screenshots.length) {
					if ((video_screenshots[0].type).indexOf("image/") === -1) {
						falg3 = 2;
						swal("请上传图片!");
						return false;
					}
				}else{
					falg3 = 2;
				}
			}
			
			if(shipin.length===0){
				if(video.length){
					if ((video[0].type).indexOf("video/") === -1) {
						falg4 = 2;
						swal("请上传视频!");
						return false;				
					}
				}else{
					falg4 = 2;
				}
			}

			if ((falg0==2 && falg1==2 && falg2==2) && (falg3==2 && falg4==2)){
                swal("视频或者图片至少上传一种!");
                return false;				
			}
			
			document.getElementById("formid").submit();
			
        });
		
        function picDel(sid) {
            swal({
                    title: "",
                    text: "确定删除么？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    $.ajax({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: "/zixun/zxpicdel/" + sid,
                        type: "POST",
                        error: function (e) {
                            if (e.readyState === 4 && e.status === 422) {
                                for (var i in e.responseJSON) {
                                    if (e.responseJSON[i] !== 'The given data was invalid.') {
                                        err = e.responseJSON[i];
                                        for (var j in err) {
                                            toasterror(err[j][0]);
                                        }
                                    }
                                }
                            }
                        },

                        success: function (data) {
                            if (data.status !== 1) {
                                swal(data.message);
                            } else {
                                swal(data.message, '', 'success');
								
                                $('.confirm').click(function () {
                                    location.reload();
                                });
                            }
                        }
                    })
                }
            );
        }

        $(document).ready(function () {
            var hidden = $('#hidden').val();
            if (hidden === 'hidden') {
                if ($('#status').val() == 1) {
                    toastsuccess($('#message').val());
                } else {
                    toasterror($('#message').val());
                }
            }
        });
    </script>
@endsection